/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.libraries.rabbitmq;

import it.txt.access.capability.commons.signer.model.X509DocumentSignerInfo;
import it.txt.access.capability.libraries.rabbitmq.exception.AbstractApplicationException;
import it.txt.access.capability.libraries.rabbitmq.impl.subscriber.SubscriberAppModel;
import it.txt.access.capability.libraries.rabbitmq.impl.model.AbstractApplicationModel;
import it.txt.access.capability.libraries.rabbitmq.impl.publisher.PublisherAppModel;
import it.txt.access.capability.libraries.rabbitmq.impl.la_publisher.LAPublisherAppModel;
import it.txt.access.capability.libraries.rabbitmq.settings.LAPublisherSettings;
import it.txt.access.capability.libraries.rabbitmq.settings.PublisherSettings;
import it.txt.access.capability.libraries.rabbitmq.settings.AbstractApplicationSettings;
import it.txt.access.capability.libraries.rabbitmq.settings.SubscriberSettings;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

/**
 *
 * @author seccia
 */
public class AbstractApplicationHelper {

    public static AbstractApplicationModel getAbstractRabbitMQAppModel(AbstractApplicationSettings settings)
            throws AbstractApplicationException {
        try {
            AbstractApplicationModel appModel = null;
            if (settings instanceof LAPublisherSettings) {
                LAPublisherSettings laps = (LAPublisherSettings) settings;
                appModel = getLogAndAuditPublisherParams(laps);
            }
            else if (settings instanceof PublisherSettings) {
                PublisherSettings ps = (PublisherSettings) settings;
                appModel = getNewPublisherRabbitMQAppModelParams(ps);
            }
            else {
                SubscriberSettings sap = (SubscriberSettings) settings;
                appModel = getNewSubscriberRabbitMQAppModelParams(sap);
            }
            ////////////////////////////////////////////////////////////////////
            appModel.setGuestBrokerHost(settings.getGuestBrokerHost());
            appModel.setGuestBrokerPort(settings.getGuestBrokerPort());
            appModel.setGuestBrokerVHost(settings.getGuestBrokerVHost());
            appModel.setGuestCredentialUser(settings.getGuestBrokerUserName());
            appModel.setGuestCredentialPwd(settings.getGuestBrokerUserPsw().toCharArray());
            appModel.setGuestENSAuthServiceMBX(settings.getEnsAuthServiceMBX());
            ////////////////////////////////////////////////////////////////////
            appModel.setCapabilitiesLocation(settings.getCapabilityLocation());
            appModel.setCapabilityHashCode(settings.getCapabilityHash());
            appModel.setSessinDeadline(getStandarSessionDeadlineDate());
            appModel.setSignerInfo(getX509DocumentSignerInfo(settings));
            ////////////////////////////////////////////////////////////////////            
            return appModel;
        } catch (AbstractApplicationException ex) {
            throw new AbstractApplicationException(ex.getMessage(), ex);
        }
    }

    private static LAPublisherAppModel getLogAndAuditPublisherParams(LAPublisherSettings settings)
            throws AbstractApplicationException {
        LAPublisherAppModel params = new LAPublisherAppModel();
        try {
            if (settings.getEntityDataURI() != null && !settings.getEntityDataURI().equals("")) {
                params.setEntityDataURI(new URI(settings.getEntityDataURI()));
            }
            if (settings.getEntityURI() != null && !settings.getEntityURI().equals("")) {
                params.setEntityURI(new URI(settings.getEntityURI()));
            }
        } catch (URISyntaxException ex) {
            throw new AbstractApplicationException("The entity URI of the publisher with ID: "
                    + settings.getApplicationID() + " is not an URI", ex);
        }
        params.setLogAndAuditNodeID(settings.getLogAndAuditNodeID());
        return params;
    }
    
    private static PublisherAppModel getNewPublisherRabbitMQAppModelParams(PublisherSettings settings)
            throws AbstractApplicationException {
        PublisherAppModel params = new PublisherAppModel();
        try {
            if (settings.getEntityDataURI() != null && !settings.getEntityDataURI().equals("")) {
                params.setEntityDataURI(new URI(settings.getEntityDataURI()));
            }
            if (settings.getEntityURI() != null && !settings.getEntityURI().equals("")) {
                params.setEntityURI(new URI(settings.getEntityURI()));
            }
        } catch (URISyntaxException ex) {
            throw new AbstractApplicationException("The entity URI of the publisher with ID: "
                    + settings.getApplicationID() + " is not an URI", ex);
        }
        params.setLeafNodeID(settings.getPublishNodeID());
        return params;
    }

    private static SubscriberAppModel getNewSubscriberRabbitMQAppModelParams(SubscriberSettings settings) {
        SubscriberAppModel params = new SubscriberAppModel();
        params.setResourceid(settings.getResourceID());
        return params;
    }

    private static X509DocumentSignerInfo getX509DocumentSignerInfo(AbstractApplicationSettings settings) {
        X509DocumentSignerInfo signerInfo = new X509DocumentSignerInfo();
        signerInfo.setPrivateKeyPwd(settings.getKeystorePrivateKey().toCharArray());
        signerInfo.setKeystorePwd(settings.getKeystorePassword().toCharArray());
        signerInfo.setKeystorePath(settings.getKeystorePath());
        signerInfo.setSignerID(settings.getKeystoreSignerID());
        return signerInfo;
    }

    private static Date getStandarSessionDeadlineDate() {
        //TODO - Define standard Session dead line
        return new Date();
    }
}
